﻿using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System;
using System.Collections.Generic;
using System.Text;
using Masuit.Tools.Security;

namespace UtilsBase
{

    public class UtilsMasuitRsaHelper
    {


        public void test()
        {
            //只能公钥加密，私钥解密
            UtilsMasuitRsaHelper rSAHelper = new UtilsMasuitRsaHelper();


            RsaKey rsakey = RsaCrypt.GenerateRsaKeys();

            var privateKey = rsakey.PrivateKey;
            var publicKey = rsakey.PublicKey;
            Console.WriteLine($"公钥:{privateKey}");
            Console.WriteLine($"私钥:{publicKey}");


            var data = "待加密的文字内容";
            var jiami = rSAHelper.EncryptByPrivateKey(data, publicKey);
            Console.WriteLine("加密结果:" + jiami);

            string sijiamicontent = jiami;// "y5+25O4jCNhQfDdQaGGocyuZa4LO+ZZH4Lr9yJ+hahmMxAQh4tVpAoUbFlGCF2dQKObk1UadYtqZDG4zjlt/venbeay861L9LAPPZ7rvf0rQ3QL2Gagy9IYstgru3+Oc+J1YCCJ00eHYRRoFSfMZUyd4TIeIY9+NWP4fWipLQZIklfvryLXRQ2oiQk3U9go4VJB2W9udRtu4NjJ60T2+0dtzvw7Ml6JvugHXULm4T0IwJoMx/yGrzR4tO7Mkf5XB58iltv/N3vHhsanguz9PqchKRBmxFnFV8OAZEK23iuJg87El2sTc7jFBTjDSQAVpKT0HaBhfz+hHAQBX/0rveg==";

            var jiemi = rSAHelper.DecryptByPublicKey(sijiamicontent, privateKey);
            Console.WriteLine("解密结果:" + jiemi);
        }

        /// <summary>
        /// KEY 结构体
        /// </summary>
        public struct RSAKEY
        {
            /// <summary>
            /// 公钥
            /// </summary>
            public string PublicKey { get; set; }
            /// <summary>
            /// 私钥
            /// </summary>
            public string PrivateKey { get; set; }
        }


        /// <summary>
        /// 私钥加密
        /// </summary>
        /// <param name="data">加密内容</param>
        /// <param name="privateKey">私钥（Base64后的）</param>
        /// <returns>返回Base64内容</returns>
        public string EncryptByPrivateKey(string data, string privateKey)
        {
            //非对称加密算法，加解密用


            //加密
            try
            {
                return data.RSAEncrypt(privateKey);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 私钥解密
        /// </summary>
        /// <param name="data">待解密的内容</param>
        /// <param name="privateKey">私钥（Base64编码后的）</param>
        /// <returns>返回明文</returns>
        public string DecryptByPrivateKey(string data, string privateKey)
        {

            //解密
            try
            {
                return data.RSADecrypt(privateKey);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 公钥加密
        /// </summary>
        /// <param name="data">加密内容</param>
        /// <param name="publicKey">公钥（Base64编码后的）</param>
        /// <returns>返回Base64内容</returns>
        public string EncryptByPublicKey(string data, string publicKey)
        {
            //非对称加密算法，加解密用


            //加密
            try
            {
                return data.RSAEncrypt(publicKey);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 公钥解密
        /// </summary>
        /// <param name="data">待解密的内容</param>
        /// <param name="publicKey">公钥（Base64编码后的）</param>
        /// <returns>返回明文</returns>
        public string DecryptByPublicKey(string data, string publicKey)
        {


            //解密
            try
            {
                return data.RSADecrypt(publicKey);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
